//
//  YYWelcomeViewController.swift
//  YYWeibo
//
//  Created by strongwallyi on 15/12/31.
//  Copyright © 2015年 strongwallyi. All rights reserved.
//

import UIKit
import SDWebImage

class YYWelcomeViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        if let headImageUrlString = YYUserAccountViewModel.sharedViewModel.userAccount?.avatar_large{
            headImage.sd_setImageWithURL(NSURL(string: headImageUrlString),placeholderImage: UIImage(named: "avatar_default_big"))
        }
        setupUI()
    }

    //初始化视图
    private func setupUI(){
        view.backgroundColor = UIColor(white: 247/255, alpha: 1)
        
        view.addSubview(headImage)
        view.addSubview(messageLabel)
        
        //给头像和 message 添加约束
        headImage.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view)
            make.top.equalTo(view).offset(200)
            make.size.equalTo(CGSizeMake(90, 90))
        }
        
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(headImage)
            make.top.equalTo(headImage.snp_bottom).offset(16)
        }
    }
    
    //在将要显示的时候开始执行动画
    override func viewDidAppear(animated: Bool) {
        super.viewWillAppear(animated)
        
        //更新约束
        headImage.snp_updateConstraints { (make) -> Void in
            make.top.equalTo(view).offset(100)
        }

        //执行动画
        UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.75, initialSpringVelocity: 0, options: [], animations: { () -> Void in
            
            //执行动画
            self.view.layoutIfNeeded()
            
            }) { (_) -> Void in
                //动画执行完毕要做的事情
                UIView.animateWithDuration(1, animations: { () -> Void in
                    
                    //执行动画
                    self.messageLabel.alpha = 1
                    
                    }, completion: { (_) -> Void in
                        //透明度动画执行完毕
                        //跳转控制器
                        NSNotificationCenter.defaultCenter().postNotificationName(YYSwitchRootViewControllerNotification, object: nil)
                })
        }
    }
    
    //懒加载头像和 label 的空间
    private lazy var headImage: UIImageView = {
        let result = UIImageView(image: UIImage(named: "avatar_default_big"))
        result.layer.cornerRadius = 45
        result.layer.masksToBounds = true
        return result
    }()
    
    private lazy var messageLabel: UILabel = {
        let label = UILabel(textColor: UIColor.darkGrayColor(), fontSize: 16)
        label.text = "欢迎归来"
        label.alpha = 0
        return label
    }()
    

}
